package com.zq.widget.inverted.game.role.impl.base;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;

import com.zq.widget.inverted.game.role.Role;
import com.zq.widget.inverted.game.role.RoleGroup;

/**
 * Created by zhangqiang on 2018/3/29.
 */
public abstract class DrawableRole extends RoleGroup {

    private Drawable drawable;

    public DrawableRole(Context context,Drawable drawable) {
        super(context);
        updateDrawable(drawable);
    }

    public DrawableRole(Context context, @DrawableRes int drawableResId) {
        super(context);
        updateDrawable(ContextCompat.getDrawable(context, drawableResId));
    }

    @Nullable
    protected Drawable getDrawable() {
        return drawable;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }
        int save = canvas.save();
        RectF bounds = bounds();
        canvas.translate(bounds.left, bounds.top);
        drawable.draw(canvas);
        canvas.restoreToCount(save);
    }

    protected void updateDrawable(Drawable drawable) {

        this.drawable = drawable;
        if (drawable == null) {
            return;
        }
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        Rect drawableBounds = drawable.getBounds();
        resetSize(drawableBounds.width(),drawableBounds.height());
    }
}
